package com.tenton.controller;

import com.tenton.MyException.DataNotExist;
import com.tenton.pojo.Commodity;
import com.tenton.service.CommodityService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Date: 2021/1/9
 * @Author: Tenton
 * @Description:
 */
@ApiIgnore
@Controller
@EnableSwagger2
@ApiOperation("管理员商品管理")
@RequestMapping("/admin/flatform/commodity")
public class ACommodityController {
    @Autowired
    CommodityService commodityService;

    /**
     * 获取所有商品信息
     *
     * @param model
     * @param pn
     * @return
     */
    @ApiOperation("获取所有商品信息")
    @GetMapping("/getList")
    public String getList(ModelMap model,
                          @ApiParam("页码") @RequestParam(defaultValue = "1", required = true, value = "pn") Integer pn
    ) {
        commodityService.getAll(pn, model);
        return "admin/commodity/getCommodityList";

    }

    /**
     * 根据id查询单个商品信息
     *
     * @param id
     * @param model
     * @return
     */
    @ApiOperation("根据id查询单个商品信息")
    @GetMapping("/getCommodity")
    public String getCommodity(int id, Model model) {
        commodityService.selectByPrimaryKey(id, model);
        return "admin/commodity/getCommodity";
    }

    /**
     * 多条件查询
     *
     * @param model
     * @param comName
     * @param pn
     * @return
     */
    @ApiOperation("多条件查询")
    @GetMapping("/getByParams")
    public String getByParams(ModelMap model,
                              @ApiParam("商品名称") @RequestParam(value = "comName", required = false) String comName,
                              @ApiParam("页码") @RequestParam(defaultValue = "1", required = true, value = "pn") Integer pn
    ) {
        commodityService.getByParams(comName, pn, model);
        return "admin/commodity/getCommodityByParams";
    }

    /**
     * 编辑商品信息
     *
     * @param id
     * @param model
     * @return
     */
    @ApiOperation("编辑商品信息")
    @GetMapping("/editCommodity")
    public String editCommodity(Integer id, Model model) {
        commodityService.selectByPrimaryKey(id, model);
        return "admin/commodity/editCommodity";
    }

    /**
     * 编辑商品信息
     *
     * @param id
     * @param model
     * @return
     */
    @ApiOperation("编辑商品信息")
    @GetMapping("/promotion")
    public String promotion(Integer id, Model model) {
        commodityService.selectByPrimaryKey(id, model);
        return "admin/commodity/editCommodity";
    }

    /**
     * 修改商品信息
     *
     * @param commodity
     * @param model
     * @param request
     * @param session
     * @return
     */
    @ApiOperation("修改商品信息")
    @PostMapping("/updateCommodity")
    public String updateCommodity(Commodity commodity, Model model, HttpServletRequest request, HttpSession session) {
        boolean flag = false;
        try {
            flag = commodityService.update(commodity,session);
            if (flag) {
                commodityService.selectByPrimaryKey(commodity.getId(), model);
                return "redirect:/admin/flatform/commodity/getList";
            }
        } catch (DataNotExist dataNotExist) {
            request.setAttribute("msg", "商品为空！");
            return "admin/commodity/editCommodity";
        }
        return null;
    }

    /**
     * 删除商品信息
     *
     * @param id
     * @return
     */
    @ApiOperation("根据商品Id删除商品信息")
    @GetMapping("/deleteCommodity")
    public String deleteCommodity(int id) {
        commodityService.deleteByPrimaryKey(id);
        return "redirect:/admin/flatform/commodity/getList";
    }

    /**
     * 跳转到增加页面
     *
     * @return
     */
    @ApiOperation("跳转到增加页面")
    @GetMapping("/toAdd")
    public String toAddCommodity() {
        return "admin/commodity/addCommodity";

    }

    /**
     * 先判断数据库有没有，有就更新，没有就新增
     *
     * @param commodity
     * @param request
     * @param session
     * @return
     */
    @ApiOperation("新增商品")
    @PostMapping("/insertCommodity")
    public String insertCommodity(Commodity commodity, HttpServletRequest request, HttpSession session) {
        try {
            commodityService.insert(commodity,session);
            return "redirect:/admin/flatform/commodity/getList";
        } catch (DataNotExist dataNotExist) {
            request.setAttribute("msg", "商品为空！");
            return "admin/commodity/addCommodity";
        }
    }

    /**
     * 时间格式化
     *
     * @param binder
     */
    @InitBinder
    protected void init(ServletRequestDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
    }

    /**
     * 获取商品信息
     *
     * @param id
     * @return
     */
    @ApiOperation("通过商品Id获取商品信息")
    @PostMapping("/getCommodities")
    public Commodity getCommodities(int id) {
        return commodityService.selectById(id);
    }
}
